package javamobile.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybeans.dao.DAOException;

import javamobile.model.*;

public class HomeAction extends Action{

	private DAOFactory factory;
	
	public HomeAction(Model model) {
		factory = model.getDAOFactory();
	}
	
	@Override
	public String getName() {return "home.do";}

	@Override
	public Map<String, Object> perform(Map<String, Object> input) {
		System.out.println("Performing home action!");
		Map<String, Object> output = new HashMap<String, Object>();
		List<String> errors = new ArrayList<String>();
		output.put("errors", errors);
		
		String username = (String) input.get("username");

		try {
			User user = factory.lookupUser(username);
			if (user == null) {
            errors.add("User "+username+" not found");
            return output;
			}

            Map<Integer, List<Object>> collect = factory.iCollect(user.getUserID());
            Map<Integer, List<Object>> owe = factory.iOwe(user.getUserID());
            List<ArrayList<Object>> history = factory.iHistory(user.getUserID());
            output.put("collect", collect);
            output.put("owe", owe);
            output.put("history", history);
        }
		catch (DAOException e) {
        	errors.add(e.getMessage());
        	return output;
	    }

		return output;
	}

}